﻿using System;
using System.Net;
using System.Windows.Forms;

namespace Kay.Client.Demo
{
    public partial class Login : Form
    {
        private StartUp _start;
        public Login(StartUp start)
        {
            InitializeComponent();
            _start = start;
        }

        private void Login_Load(object sender, EventArgs e)
        {
            Application.DoEvents();
            textBox_Name.Text = RandomName.GetRandomName();
        }

        private void button_Login_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            var isReturn = false;
            IPAddress ip;
            if (IPAddress.TryParse(textBox_Ip.Text, out ip))
            {
                _start.Ip = textBox_Ip.Text;
            }
            else
            {
                //MessageBox.Show(this, @"Ip 地址错误！", @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                errorProvider.SetError(textBox_Ip, "Ip 地址有误！");
                isReturn = true;
            }
            ushort port;
            if (ushort.TryParse(textBox_Port.Text, out port))
            {
                _start.Port = port;
            }
            else
            {
                //MessageBox.Show(this, @"端口无效！", @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                errorProvider.SetError(textBox_Port, "端口无效！");
                isReturn = true;
            }
            if (textBox_Name.Text.Trim() != string.Empty)
            {
                _start.UserName = textBox_Name.Text.Trim();
                _start.Text = textBox_Name.Text.Trim();
            }
            else
            {
                //MessageBox.Show(this, @"用户名无效！", @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                errorProvider.SetError(textBox_Name, "用户名无效！");
                isReturn = true;
            }
            if (isReturn)
            {
                return;
            }
            _start.Password = textBox_Pwd.Text;
            DialogResult = DialogResult.OK;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
